﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CubaLibreBar.Model.Dados;
using CubaLibreBar.Model.Entidades;
using System.Data.SqlServerCe;
using Microsoft.Reporting.WinForms;
using CrystalDecisions.Shared;

namespace CubaLibreBar.Win.Relatorios
{
    public partial class ProdutosCadastrados : UserControl
    {
        ReportDocument caixa = new ReportDocument();

        public ProdutosCadastrados()
        {
            InitializeComponent();
        }

        private void ProdutosCadastrados_Load(object sender, EventArgs e)
        {
            gbExportar.Visible = false;

            var currentDirectory = Application.StartupPath;
            caixa.Load(currentDirectory + "/Relatorios/Produtos.rpt");

            bwRelatorio.RunWorkerAsync();
        }

        private void pbExcel_Click(object sender, EventArgs e)
        {
            ReportDocument report = new ReportDocument();
            var currentDirectory = Application.StartupPath;
            report.Load(currentDirectory + "/Relatorios/Produtos.rpt");

            report.SetDataSource(GetDataSetRelatorio(report));

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
            saveFileDialog.Filter = "Document (*.xls)|*.XLS";
            saveFileDialog.FilterIndex = 1;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                ExportFormatType efileType = ExportFormatType.Excel;
                report.ExportToDisk(efileType, saveFileDialog.FileName);
            }
        }

        private void pbPdf_Click(object sender, EventArgs e)
        {
            ReportDocument report = new ReportDocument();
            var currentDirectory = Application.StartupPath;
            report.Load(currentDirectory + "/Relatorios/Produtos.rpt");

            report.SetDataSource(GetDataSetRelatorio(report));

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
            saveFileDialog.Filter = "Document (*.pdf)|*.PDF";
            saveFileDialog.FilterIndex = 1;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                ExportFormatType efileType = ExportFormatType.PortableDocFormat;
                report.ExportToDisk(efileType, saveFileDialog.FileName);
            }
        }

        private DataSet GetDataSetRelatorio(ReportDocument caixa)
        {
            DataSet ds = new DataSet();
            
            using (SqlCeConnection connection = ConnectionFactory.GetConnection())
            {
                string sql = "SELECT * FROM tbProdutos";

                using (SqlCeDataAdapter adapter = new SqlCeDataAdapter(sql, connection))
                {
                    adapter.Fill(ds, "tbProdutos");
                }
            }

            return ds;
        }

        private void bwRelatorio_DoWork(object sender, DoWorkEventArgs e)
        {
            caixa.SetDataSource(GetDataSetRelatorio(caixa));
        }

        private void bwRelatorio_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {                               
            rpvCaixa.ReportSource = caixa;
            
            gbExportar.Visible = true;
        }
    }
}
